
package com.example.service.impl;

import com.example.service.SysCuratorService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysCuratorServiceImpl implements SysCuratorService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private CuratorFramework curatorFramework;

    @Override
    public CuratorFramework getCuratorFramework() {
        return curatorFramework;
    }

    @Override
    public boolean isExistNode(String path) {
        curatorFramework.sync();
        try {
            return curatorFramework.checkExists().forPath(path) != null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void createNode(CreateMode mode, String path) {
        try {
            curatorFramework.create().creatingParentsIfNeeded().withMode(mode).forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setNodeData(String path, String nodeData) {
        try {
            curatorFramework.setData().forPath(path, nodeData.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String createNodeAndData(CreateMode mode, String path, String nodeData) {
        try {
            return curatorFramework.create().creatingParentsIfNeeded().withMode(mode).forPath(path,
                    nodeData.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getNodeData(String path) {
        try {
            byte[] dataByte = curatorFramework.getData().forPath(path);
            String data = new String(dataByte, "UTF-8");
            if (!StringUtils.isEmpty(data)) {
                return data;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<String> getNodeChild(String path) {
        List<String> nodeChildDataList = new ArrayList<>();
        try {
            nodeChildDataList = curatorFramework.getChildren().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodeChildDataList;
    }

    @Override
    public void deleteNode(String path, Boolean recursive) {
        try {
            if (recursive) {
                curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
            } else {
                curatorFramework.delete().guaranteed().forPath(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void watchNode(String path, PathChildrenCacheListener pathChildrenCacheListener) {
        PathChildrenCache pathChildrenCache = null;
        try {
            pathChildrenCache = new PathChildrenCache(curatorFramework, path, true);
            pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
            pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
        } catch (Exception e) {
            if (pathChildrenCache != null) {
                try {
                    pathChildrenCache.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
